How to install docker on ubuntu using ansible?
Installation of docker on Ubuntu using ansible
Below are the steps to be followed to install docker on ubuntu using ansible
- Create
a new Ansible playbook file, for example docker_setup.yml.
- Define
the hosts or host group on which you want to install Docker. For example,
if you want to install Docker on a group of Ubuntu servers with the tag docker_servers,
you can define it like this:
- name: Install Docker on Ubuntu
servers
hosts: docker_servers
- Define
the tasks required to install and set up Docker:
- name: Install Docker on Ubuntu
servers
hosts: docker_servers
- name: Install aptitude
apt: name=aptitude
state=present
- name: Install required system
packages
apt: name={{ item }} state=present
with_items:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
- name: Install Docker GPG APT
key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository to
apt sources
apt_repository:
repo: deb [arch=amd64]
https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present
- name: Install Docker
apt: name=docker-ce
state=present
- name: Install Python Docker
module via pip
pip:
name: docker
- name: Pull default Docker image
docker_image:
name: "{{ default_container_image }}"
state: present
- name: Create Docker containers
docker_container:
name: "{{ default_container_name }}{{ item }}"
image: "{{ default_container_image }}"
command: "{{ default_container_command }}"
state: started
with_sequence: start=1 count={{ container_count }}
The above playbook consists of several tasks, each of which
performs a specific action:
- The
first task installs the aptitude package manager, which is
preferred by Ansible as an alternative to apt.
- The
second task installs several required system packages, including apt-transport-https,
ca-certificates, curl, gnupg-agent, and software-properties-common.
- The
third task installs the Docker GPG APT key, which is necessary to
authenticate the Docker packages.
- The
fourth task adds the official Docker repository to the server's APT
sources.
- The
fifth task installs Docker itself using the apt package manager.
- The
sixth task installs the Python Docker module via pip.
- The
seventh task pulls the default Docker image specified by the default_container_image
variable from Docker Hub.
- The
eighth task creates the number of Docker containers defined by the container_count
variable, each using the image defined by default_container_image,
and executes the command defined in default_container_command in
each new container.
This playbook uses several Ansible modules to perform these
tasks, including apt, apt_key, apt_repository, pip,
docker_image, and docker_container. The with_items and with_sequence
directives are used to loop through lists of items and perform the same task
multiple times with different parameters.
- Define
the variables required for the playbook to run. You can define these
variables in a separate file and include it in the playbook, or you can
define them inline in the playbook. Here's an example of defining
variables inline:
- name: Install Docker on Ubuntu
servers
hosts: docker_servers
vars:
default_container_image: ubuntu:latest
default_container_name: my_container
default_container_command: /bin/bash
container_count: 3
In this example, we're using the ubuntu:latest image,
naming our containers my_container, running the /bin/bash command
in each container, and creating 3 containers.
- Run
the playbook using the ansible-playbook command:
ansible-playbook
docker_setup.yml
Below is the kind of output
observed when above command is executed
$ ansible-playbook
docker_setup.yml
PLAY [Install Docker on
Ubuntu servers]
*********************************************************************
TASK [Gathering Facts]
**************************************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Install aptitude]
*************************************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Install required
system packages] *********************************************************************
ok: [server1] =>
(item=apt-transport-https)
ok: [server2] =>
(item=apt-transport-https)
ok: [server3] =>
(item=apt-transport-https)
ok: [server1] =>
(item=ca-certificates)
ok: [server2] =>
(item=ca-certificates)
ok: [server3] =>
(item=ca-certificates)
ok: [server1] =>
(item=curl)
ok: [server2] =>
(item=curl)
ok: [server3] =>
(item=curl)
ok: [server1] =>
(item=gnupg-agent)
ok: [server2] =>
(item=gnupg-agent)
ok: [server3] =>
(item=gnupg-agent)
ok: [server1] =>
(item=software-properties-common)
ok: [server2] =>
(item=software-properties-common)
ok: [server3] =>
(item=software-properties-common)
TASK [Install Docker GPG
APT key]
***************************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Add Docker repository
to apt sources] ****************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Install Docker]
**************************************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Install Python Docker
module via pip] ****************************************************************
ok: [server1]
ok: [server2]
ok: [server3]
TASK [Pull default Docker
image]
***************************************************************************
changed: [server1]
changed: [server2]
changed: [server3]
TASK [Create Docker
containers]
****************************************************************************
changed: [server1] =>
(item=1)
changed: [server1] =>
(item=2)
changed: [server1] =>
(item=3)
changed: [server2] =>
(item=1)
changed: [server2] =>
(item=2)
changed: [server2] =>
(item=3)
changed: [server3] =>
(item=1)
changed: [server3] =>
(item=2)
changed: [server3] =>
(item=3)
PLAY RECAP
*************************************************************************************************
server1 : ok=8 changed=2
unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
server2 : ok=8 changed=2
unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
server3 : ok=8 changed=2
unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
Comments
Post a Comment